* This file produces Table 1
set more off

use "$saveddata/data_complete_withcosts.dta", clear

rename spell_los inpat_los
rename number_proc inpat_proc

set more off
* Input statistics into a table
putexcel set "$results/Table1.xlsx", replace

putexcel B1 = "All patients"
putexcel E1 = "Inpatient only"

putexcel B2 = "Mean"
putexcel C2 = "Standard Deviation"
putexcel E2 = "Mean"
putexcel F2 = "Standard deviation"

* Patient characteristics
putexcel A3 = "Patient characteristics"

local y=4
foreach var in d_age d_male d_ambulance charlindex {

putexcel A`y' = "`var'"

su `var'
matrix a = r(mean)
putexcel B`y' = matrix(a)

matrix b = r(sd)'
putexcel C`y' = matrix(b)

su `var' if admit==1
matrix c = r(mean)'
putexcel E`y' = matrix(c)

matrix d = r(sd)'
putexcel F`y' = matrix(d)

local y=`y'+1
}

putexcel A`y' = "Treatment decisions"
local y=`y'+1

foreach var in admit discharge othexit depdur tcount icount inpat_los inpat_proc costae30_all costem30_all cost30_aeem{

putexcel A`y' = "`var'"

su `var'
matrix a = r(mean)
putexcel B`y' = matrix(a)

matrix b = r(sd)'
putexcel C`y' = matrix(b)

su `var' if admit==1
matrix c = r(mean)'
putexcel E`y' = matrix(c)

matrix d = r(sd)'
putexcel F`y' = matrix(d)

local y=`y'+1
}

putexcel A`y' = "Outcomes"
local y=`y'+1

foreach var in death30 death90 death365{

putexcel A`y' = "`var'"

su `var'
matrix a = r(mean)
putexcel B`y' = matrix(a)

matrix b = r(sd)'
putexcel C`y' = matrix(b)

su `var' if admit==1
matrix c = r(mean)'
putexcel E`y' = matrix(c)

matrix d = r(sd)'
putexcel F`y' = matrix(d)

local y=`y'+1
}
